//
//  YYMusicPlayListView.swift
//  STMusic
//
//  Created by stoneobs on 2017/6/13.
//  Copyright © 2017年 stoneobs. All rights reserved.
//
//**********************乐文中的播放列表***************************
import UIKit

protocol YYMusicPlayListViewDelegate:NSObjectProtocol {
    //点击了播放按钮
    func didClicThePlayView(listView:YYMusicPlayListView) -> Void ;
    //点击了展开按钮
    func didClicTheShowAllView(listView:YYMusicPlayListView) -> Void ;
    //点击了展开列表
    func didClicTheAllView(listView:YYMusicPlayListView,tag:Int) -> Void ;
}
class YYMusicPlayListView: UIView {

    var model:YYArticleDetailModel? = YYArticleDetailModel(){
    
        didSet{
        
            self.bannerImageView?.sd_setImage(with: URL.init(string: (model?.track.songphoto)!), placeholderImage: BannerImage, options: .retryFailed);
            
            self.titleLable?.text = model?.track.songname;
            
            self.iconImageView?.sd_setImage(with: URL.init(string: (model?.author?.pimg)!), placeholderImage: PeopleImage, options:  .retryFailed);

            self.nickNameLable?.text = model?.track.songer;
            self.nickNameLable?.sizeToFit();
            
            self.listenButton?.left = (self.nickNameLable?.right)! + 10;
            self.listenButton?.setTitle(model?.track.play_count, for: .normal);
            let counts:String = String((model?.tracks?.count)! );
            self.allButton?.setTitle("全部" + counts + "首" , for: .normal);
            
        }
    }
    
    weak var delegate:YYMusicPlayListViewDelegate?;
    
    
    //ui
    var playButton:STButton?
    
    var titleLable:STLabel?
    
    var iconImageView:UIImageView?
    
    var nickNameLable:STLabel?
    
    var bannerImageView:UIImageView?
    
    
    var listenButton:STButton?
    
    var allButton:STButton?
    
    
    
    private var listMusicView:NSMutableArray = [];
    
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        initSubview()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initSubview() {
        
        self.backgroundColor = BACKROUND_COLOR;
        weak var weakself = self ;
        self.bannerImageView = UIImageView.init(frame: .init(x: 0, y: 0, width: self.height , height: self.height ));
        self.bannerImageView?.isUserInteractionEnabled = true;
        self.addSubview(self.bannerImageView!);
        self.playButton = STButton.init(frame: .init(x: 0, y: 0, width: 32, height: 32),
                                        title: "",
                                        titleColor: nil,
                                        titleFont: 0,
                                        cornerRadius: 0,
                                        backgroundColor: .clear,
                                        backgroundImage: nil,
                                        image: UIImage.init(named: ""));

        self.playButton?.center = CGPoint.init(x: self.height / 2, y: self.height / 2);
        self.playButton?.setBackgroundImage(UIImage.init(named: "btn_play"), for: .normal);
        self.playButton?.setBackgroundImage(UIImage.init(named: "btn_pause"), for: .selected);
        self.playButton?.contentHorizontalAlignment = .center;
        self.playButton?.clicAction = {but in
            weakself?.playButton?.isSelected = !(weakself?.playButton?.isSelected)!;
            weakself?.delegate?.didClicThePlayView(listView: weakself!);
        }
        self.bannerImageView?.addSubview(self.playButton!);
        
        
        self.titleLable = STLabel.init(frame: .init(x: (self.bannerImageView?.right)! + 10, y: 10, width: self.width - self.height - 20, height: 16),
                                       text: "古风岭",
                                       textColor: FirstTextColor,
                                       font: 15,
                                       isSizetoFit: false,
                                       textAlignment: .left);
        
        self.addSubview(self.titleLable!);
        

        self.iconImageView = UIImageView.init(frame: .init(x: (self.bannerImageView?.right)! + 10, y: (self.titleLable?.bottom)! + 10, width: 18, height: 18));
        self.iconImageView?.layer.cornerRadius = 9;
        self.iconImageView?.clipsToBounds = true;
        self.addSubview(self.iconImageView!);
        
        self.nickNameLable = STLabel.init(frame: .init(x: (self.iconImageView?.right)! + 3, y: (self.titleLable?.bottom)! + 10, width: self.width - self.height - 20, height: 13),
                                          text: "古风岭",
                                          textColor: SecendTextColor,
                                          font: 12,
                                          isSizetoFit: false,
                                          textAlignment: .left);
        self.nickNameLable?.centerY = (self.iconImageView?.centerY)!;
        self.addSubview(self.nickNameLable!);
        
        self.listenButton = STButton.init(frame: .init(x: (self.nickNameLable?.right)! + 10, y: (self.titleLable?.bottom)! + 10, width: 80 , height: 13),
                                        title: "2344",
                                        titleColor: nil,
                                        titleFont: 12,
                                        cornerRadius: 0,
                                        backgroundColor: .clear,
                                        backgroundImage: nil,
                                        image: UIImage.init(named: "btn_listen"));
        self.listenButton?.setTitleColor(SecendTextColor, for: .normal);
        self.listenButton?.centerY = (self.iconImageView?.centerY)!;
        self.listenButton?.titleEdgeInsets = .init(top: 0, left: 3, bottom: 0, right: 0);
        self.addSubview(self.listenButton!);
        
        
        self.allButton = STButton.init(frame: .init(x: self.width - 110 , y: (self.titleLable?.bottom)! + 10, width: 100 , height: 13),
                                          title: "全部11首",
                                          titleColor: nil,
                                          titleFont: 12,
                                          cornerRadius: 0,
                                          backgroundColor: .clear,
                                          backgroundImage: nil,
                                          image: UIImage.init(named: "btn_pullDown"));
        self.allButton?.contentHorizontalAlignment = .center;
        self.allButton?.setTitleColor(SecendTextColor, for: .normal);
        self.allButton?.centerY = (self.iconImageView?.centerY)!;
        self.allButton?.setImage(UIImage.init(named: "btn_pullDown"), for: .normal);
        self.allButton?.setImage(UIImage.init(named: "btn_pullUp"), for: .selected);
        self.allButton?.titleEdgeInsets = .init(top: 0, left: 5, bottom: 0, right: 0);
        self.allButton?.clicAction = {but in
            weakself?.allButton?.isSelected = !(weakself?.allButton?.isSelected)!;
            if (weakself?.allButton?.isSelected)! {
                weakself?.showListView();
            }else{
                weakself?.hideListView();
            }
            weakself?.delegate?.didClicTheShowAllView(listView: weakself!);
        }
        self.addSubview(self.allButton!);
        
        
    }
    func showListView() -> Void {
        
        for i in 0...(self.model?.tracks?.count)! - 1{
        
            let cell = YYMusicListCellView.init(frame: CGRect.init(x: 0, y: self.height , width: self.width, height: 40));
            cell.model = self.model?.tracks?[i];
            cell.tag = i + 1;
            
            self.showTransAnimationWithTime(type: .STkCATransitionFade,duration:0.6);
            self.addSubview(cell);
            weak var weakSelf = self;
            cell.didClicAction = { [weak cell] myview in
            
                weakSelf?.delegate?.didClicTheAllView(listView: weakSelf!, tag: (cell?.tag)!);
            
            }
            self.height = self.height + 40;
            self.listMusicView.add(cell);
            
        
        }
    }
    func hideListView() -> Void {
        
        let myarray:NSArray = NSArray.init(array: self.listMusicView);

        for  view in myarray {
            let  myview = view as! UIView;
            myview.removeFromSuperview();
            self.listMusicView.remove(myview);
            self.height = self.height - 40;
            self.showTransAnimationWithTime(type: .STrippleEffect,duration:0.6);
        }
        
    }

}

class YYTitleView: UIView {
    var model:YYArticleDetailModel? = YYArticleDetailModel(){
    
        didSet{
        
            
            
            self.titleLable?.text = model?.title?.components(separatedBy: "：").last;
            
            self.authorLable?.text = "文/" + (model?.editor)!;
            
            
            self.scanButton?.setTitle(model?.total_play_count?.description, for: .normal);
        
        }
    }
    
    var titleLable:STLabel?
    
    var authorLable:STLabel?

    var scanButton:STButton?
    override init(frame: CGRect) {
        super.init(frame: frame)
        initSubview()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initSubview() -> Void {
        
        self.titleLable = STLabel.init(frame: .init(x: 0, y: 3, width: self.width , height: 17),
                                       text: "古风岭",
                                       textColor: FirstTextColor,
                                       font: 16,
                                       isSizetoFit: false,
                                       textAlignment: .left);
        self.addSubview(self.titleLable!);
        
        self.authorLable =  STLabel.init(frame: .init(x: 0, y: (self.titleLable?.bottom)! + 10, width: 100 , height: 13),
                                         text: "古风岭",
                                         textColor: SecendTextColor,
                                         font: 12,
                                         isSizetoFit: false,
                                         textAlignment: .left);
        self.addSubview(self.authorLable!);
        
        self.scanButton =  STButton.init(frame: CGRect.init(x: (self.authorLable?.right)! + 15, y: (self.titleLable?.bottom)! + 10, width: 70, height: 14),
                                         title: "12300",
                                         titleColor: SecendTextColor,
                                         titleFont: 13,
                                         cornerRadius: 0,
                                         backgroundColor: .clear,
                                         backgroundImage: nil,
                                         image: UIImage.init(named: "btn_scan"));
        self.scanButton?.contentHorizontalAlignment = .left;
        self.addSubview(self.scanButton!);
        
        
    }
}

class YYMusicListCellView: UIView {
    var nameLable:STLabel?
    
    var listenButton:STButton?
    
    var  model:YYTrack? = YYTrack(){
    
        didSet{
        
            self.nameLable?.text = model?.songname;
            
            self.listenButton?.setTitle(model?.play_count, for: .normal);
        }
    }
    var didClicAction:(_ listCell:YYMusicListCellView) -> Void = {_ in };
    override init(frame: CGRect) {
        super.init(frame: frame)
        initSubview()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initSubview() -> Void {
        
        self.backgroundColor = FlatWhiteDark();
        
        self.nameLable = STLabel.init(frame: .init(x: 10, y: 3, width: self.width - 20 - 90 , height: 15),
                                       text: "古风岭",
                                       textColor: SecendTextColor,
                                       font: 14,
                                       isSizetoFit: false,
                                       textAlignment: .left);
        self.nameLable?.centerY = self.height / 2;
        self.addSubview(self.nameLable!);
        

        
        self.listenButton =  STButton.init(frame: CGRect.init(x: self.width - 80, y: 0, width: 70, height: 14),
                                         title: "12300",
                                         titleColor: SecendTextColor,
                                         titleFont: 13,
                                         cornerRadius: 0,
                                         backgroundColor: .clear,
                                         backgroundImage: nil,
                                         image: UIImage.init(named: "btn_listen"));
        self.listenButton?.contentHorizontalAlignment = .right;
        self.listenButton?.centerY = self.height / 2;
        self.addSubview(self.listenButton!);
        
        let line:UIView = UIView.init(frame: .init(x: 0, y: self.height - 1, width: self.width, height: 1));
        line.backgroundColor = BACKROUND_COLOR;
        self.addSubview(line);
        
        let tapges:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(didClicTheCell(cell:)));
        
        self.isUserInteractionEnabled = true;
        self.addGestureRecognizer(tapges);
        
        
        
    }
    func didClicTheCell(cell:YYMusicListCellView) -> Void {
        
        self.backgroundColor = FlatWhite();
        self.showTransAnimation(type: .STkCATransitionReveal);
        self.backgroundColor = FlatWhiteDark();
        self.didClicAction(cell);
    }
    
    
}
